package com.banciyuan.bcywebview.biz.main.mineinfo.mywork;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.base.helper.timelinedatacenter.DetailDataHelper;
import com.banciyuan.bcywebview.utils.universalimageloader.core.ImageLoader;
import com.banciyuan.bcywebview.utils.universalimageloader.utils.ImageOptions;

import java.util.List;

import de.greenrobot.daoexample.model.MainWork;
import de.greenrobot.daoexample.model.NativeMainWork;
import de.greenrobot.daoexample.model.WorkMonth;

/**
 * Created by hy on 2016/3/22.
 */
public class MainWorkAdapter extends BaseAdapter {
    private Context context;
    private List<NativeMainWork> nativeMainWorkList;
    private ImageLoader imageLoader;
    private LayoutInflater mInflater;
    private String CHARPTER_COUNT_FORMAT;

    public MainWorkAdapter(Context context, List<NativeMainWork> nativeMainWorkList) {
        this.context = context;
        this.nativeMainWorkList = nativeMainWorkList;
        mInflater = LayoutInflater.from(context);
        imageLoader = ImageLoader.getInstance();
        CHARPTER_COUNT_FORMAT = context.getString(R.string.charpter_count);
    }

    @Override
    public int getCount() {
        return nativeMainWorkList.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        int viewType = getItemViewType(position);
        NativeMainWork nativeMainWork = nativeMainWorkList.get(position);
        switch (viewType) {
            case 0:
                TimeViewHolder timeViewHolder;
                WorkMonth workMonth = nativeMainWork.getWorkMonth();
                if (null == convertView) {
                    timeViewHolder = new TimeViewHolder();
                    convertView = mInflater.inflate(R.layout.mainwork_time_item, parent, false);
                    timeViewHolder.tvTime = (TextView) convertView.findViewById(R.id.tv_mainwork_time);
                    timeViewHolder.tvCount = (TextView) convertView.findViewById(R.id.tv_mainwork_count);
                    convertView.setTag(timeViewHolder);
                } else {
                    timeViewHolder = (TimeViewHolder) convertView.getTag();
                }
                timeViewHolder.tvCount.setText(String.format(CHARPTER_COUNT_FORMAT, workMonth.getCount()));
                timeViewHolder.tvTime.setText(workMonth.getDate());
                return convertView;
            default:
                ViewHolder mViewHolder;
                if (null == convertView) {
                    mViewHolder = new ViewHolder();
                    convertView = mInflater.inflate(R.layout.mainwork_item, parent, false);
                    mViewHolder.picOne = (ImageView) convertView.findViewById(R.id.siv_pic_one);
                    mViewHolder.picTwo = (ImageView) convertView.findViewById(R.id.siv_pic_two);
                    mViewHolder.picThree = (ImageView) convertView.findViewById(R.id.siv_pic_three);
                    convertView.setTag(mViewHolder);
                } else {
                    mViewHolder = (ViewHolder) convertView.getTag();
                }
                final MainWork oneMainWork = nativeMainWork.getOneMainWork();
                if (null != oneMainWork)
                    imageLoader.displayImage(oneMainWork.getImg_src(), mViewHolder.picOne, ImageOptions.options);
                final MainWork twoMainWork = nativeMainWork.getTwoMainWork();
                if (null != twoMainWork) {
                    mViewHolder.picTwo.setVisibility(View.VISIBLE);
                    imageLoader.displayImage(twoMainWork.getImg_src(), mViewHolder.picTwo, ImageOptions.options);
                } else
                    mViewHolder.picTwo.setVisibility(View.INVISIBLE);
                final MainWork threeMainWork = nativeMainWork.getThreeMainWork();
                if (null != threeMainWork) {
                    mViewHolder.picThree.setVisibility(View.VISIBLE);
                    imageLoader.displayImage(threeMainWork.getImg_src(), mViewHolder.picThree, ImageOptions.options);
                } else
                    mViewHolder.picThree.setVisibility(View.INVISIBLE);
                mViewHolder.picOne.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        try {
                            if (!TextUtils.isEmpty(oneMainWork.getCp_id())) {
                                DetailDataHelper.gotoCosDetail(context, oneMainWork.getRp_id(), oneMainWork.getCp_id(), false);
                            } else {
                                DetailDataHelper.gotoDrawerDetail(context, oneMainWork.getRp_id(), oneMainWork.getDp_id(), false);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
                mViewHolder.picTwo.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        try {
                            if (!TextUtils.isEmpty(twoMainWork.getCp_id())) {
                                DetailDataHelper.gotoCosDetail(context, twoMainWork.getRp_id(), twoMainWork.getCp_id(), false);
                            } else {
                                DetailDataHelper.gotoDrawerDetail(context, twoMainWork.getRp_id(), twoMainWork.getDp_id(), false);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
                mViewHolder.picThree.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        try {
                            if (!TextUtils.isEmpty(threeMainWork.getCp_id())) {
                                DetailDataHelper.gotoCosDetail(context, threeMainWork.getRp_id(), threeMainWork.getCp_id(), false);
                            } else {
                                DetailDataHelper.gotoDrawerDetail(context, threeMainWork.getRp_id(), threeMainWork.getDp_id(), false);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
                return convertView;
        }
    }

    static class ViewHolder {
        ImageView picOne;
        ImageView picTwo;
        ImageView picThree;
    }

    static class TimeViewHolder {
        TextView tvTime;
        TextView tvCount;
    }

    @Override
    public int getItemViewType(int position) {
        NativeMainWork nativeMainWork = nativeMainWorkList.get(position);
        if (0 == nativeMainWork.getType()) {
            return 0;
        } else {
            return 1;
        }
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    public void setNativeMainWorkList(List<NativeMainWork> nativeMainWorkList) {
        this.nativeMainWorkList = nativeMainWorkList;
    }
}
